#ifndef FILEFUNCTION_H_
#define FILEFUNCTION_H_



#include "BasicConfig.h"


static inline bool file_put_contents_append(QString fullFileName,QString xml)
{
    QString data = xml+"\n";
    QFile f( fullFileName );
	if ( f.open( QFile::Append | QFile::Text ) )
	{
		QTextStream sw( &f );
		sw << data;
		f.close();
		return true;
	}
	return false;
    
}

/* check if a file exist alert dons say true if a zero byte file!  */
static inline bool is_file(QString fullFileName)
{
    if (!fullFileName.size() > 3) {
      return false;  
    }
    QFile f( fullFileName );
	if ( f.exists(fullFileName) ) {
    return true;  
	} else {
	return false;
    }
}


/* simple mkdir */
static inline bool Cache( const QString dirpath )
{
    QDir dira(dirpath);
    if ( dira.mkpath(dirpath) ) {
    return true;
    } else {
    return false;
    }
}
/* os path from sistem */
extern inline QString PathConvert( QString path ) 
{
    QString resolvepath;
    #if defined Q_WS_WIN
    //////resolvepath = QDir::convertSeparators(path);
    resolvepath = path.replace('/','\\');
    #endif
    resolvepath = path;
   ///////////// qDebug() << "### resolvepath " << resolvepath; 
    return resolvepath;
}


/* allowed char on file name to save */
static inline QString FilenameAllow( QString touri )
{
    touri = touri.replace(" ","_");
    QStringList piece = touri.split("");
    QString alloweduri = "abcdefghijklmnopqrstuvwxyz1234567890_.-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    QStringList alist = alloweduri.split("");
    
     for (int i = 0; i < piece.size(); ++i) {
           QString pin = piece.at(i);
           if (!alist.contains(pin)) {
               touri.replace(pin,"");
           }
       }
    
    return touri.trimmed();
}

/* fop file grep url from inline image */
static inline QString ImagesrcUrl( const QDomElement e )
{
    QString hrefimageplace = e.attribute( "src" ,"");
    if (hrefimageplace.startsWith("url(",Qt::CaseInsensitive)) {
        hrefimageplace = hrefimageplace.right(hrefimageplace.size() - 4);
        hrefimageplace = hrefimageplace.left(hrefimageplace.size() - 1).trimmed();
    }
    if (hrefimageplace.startsWith("file:",Qt::CaseInsensitive)) {
        hrefimageplace =  hrefimageplace.replace("file:","",Qt::CaseInsensitive);
    }
    return hrefimageplace;
}


/* check if a net file to download */
static inline bool IsNetFile( QString fullFileName )
{
    if (fullFileName.startsWith("http://", Qt::CaseInsensitive) || 
        fullFileName.startsWith("https://", Qt::CaseInsensitive) ||
        fullFileName.startsWith("ftp://", Qt::CaseInsensitive) ||
        fullFileName.startsWith("webdav://", Qt::CaseInsensitive)  )
       { 
         return true;
       } else {
         return false;  
       }
}


/* read the contenet of a local file as qstring direct utf-8 */
static inline QString fopenutf8(QString fullFileName)
{
    QString inside = "";
    QFile file(fullFileName); 
    if (file.exists()) {
                if (file.open(QFile::ReadOnly | QFile::Text)) {
                    inside = QString::fromUtf8(file.readAll());
                  file.close();
                }
    }

return inside;
}


/* write a file to utf-8 format */
static inline bool fwriteutf8(QString fullFileName,QString xml)
{
    if (fullFileName.contains("/", Qt::CaseInsensitive)) {
    QString ultimacartellaaperta = fullFileName.left(fullFileName.lastIndexOf("/"))+"/";
    QDir dira(ultimacartellaaperta);
    if ( dira.mkpath(ultimacartellaaperta) ) { } else {
    return false;
    }
    }
    
        QTextCodec *codecx;
        codecx = QTextCodec::codecForMib(106);
        QFile f( fullFileName );
        if ( f.open( QFile::WriteOnly | QFile::Text ) )
        {
        QTextStream sw( &f );
        sw.setCodec(codecx);
        sw << xml;
        f.close();
        return true;
        }
        return false;
}


/* write a file to utf-8 format */
static inline bool fwriter(QString fullFileName,QString xml)
{
    return fwriteutf8(fullFileName,xml);
}





static inline void OpenDesk( QUrl loc )
{
    #if defined Q_WS_MAC
    QProcess *m = new QProcess();
    QStringList macs;
    macs << loc.toString();  /* oeffnet der default browser */
    m->startDetached(QString("open") , macs );
    ///////std::cout << "OpenDesktop [" << qPrintable(macs.join(" ")) << "]" << std::endl;
    return;
    #endif 
    
#if defined Q_WS_WIN
    
    QString fullFileName = loc.toString();
    
    if (fullFileName.startsWith("http://", Qt::CaseInsensitive) ||
        fullFileName.startsWith("https://", Qt::CaseInsensitive) ||
        fullFileName.startsWith("ftp://", Qt::CaseInsensitive) ||
        fullFileName.startsWith("news://", Qt::CaseInsensitive) ||
        fullFileName.startsWith("mailto:", Qt::CaseInsensitive) ||
        fullFileName.startsWith("webdav://", Qt::CaseInsensitive)  )
       { 
         /* all ok */
       } else {
         fullFileName.prepend("file:///");  
       }
    bool wr = QDesktopServices::openUrl(QUrl(fullFileName));
    if (!wr) {
    QMessageBox::warning(0, qApp->tr("Error"),qApp->tr("Window Unable to open action file or dir  %1").arg(loc.toString()));
    }
return;
#endif
    
    /* linux */

     bool r = QDesktopServices::openUrl(loc);
    if (!r) {
    QMessageBox::warning(0, qApp->tr("Error"),qApp->tr("Linux Unable to open action file or dir  %1").arg(loc.toString()));
    }
    
}



static inline QByteArray ReadFileByte( const QString file ) 
{
        QFile putfile( file );
        if(!putfile.open(QIODevice::ReadOnly ) ) {
        return QByteArray();
        } else {
            QByteArray data = putfile.readAll();
            putfile.close();
         return  data;
        }
}








#endif /*FILEFUNCTION_H_*/
